package com.hyk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyk.domain.CoinRecharge;
import com.hyk.dto.UserDto;
import com.hyk.feign.UserServiceFeign;
import com.hyk.mapper.CoinRechargeMapper;
import com.hyk.service.CoinRechargeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CoinRechargeServiceImpl extends ServiceImpl<CoinRechargeMapper, CoinRecharge> implements CoinRechargeService{

    @Autowired
    private UserServiceFeign userServiceFeign;

    @Resource
    private CoinRechargeMapper coinRechargeMapper;

    @Override
    public Page<CoinRecharge> findByPage(Page<CoinRecharge> page, Long coinId, Long userId, String userName, String mobile, Byte status, String numMin, String numMax, String startTime, String endTime) {
        List<Long> ids = new ArrayList<>();
        Map<Long, UserDto> user = new HashMap<>();
        if(userId != null ||
                StringUtils.isNotBlank(userName) ||
                StringUtils.isNotBlank(mobile)){
            ArrayList<Long> userIds = new ArrayList<>();
            if(userId!=null){
                userIds.add(userId);
            }
            user = userServiceFeign.getBasicUsers(userIds.size() == 0 ? null : userIds,mobile,userName);
            Set<Map.Entry<Long, UserDto>> set = user.entrySet();
            for(Map.Entry<Long, UserDto> map:set){
                ids.add(map.getKey());
            }
            if(CollectionUtils.isEmpty(ids)){
                return page;
            }
        }

        Page<CoinRecharge> result = coinRechargeMapper.findByPage(page,ids,status,numMin,numMax,startTime,endTime);
        List<CoinRecharge> records = result.getRecords();
        if (CollectionUtils.isEmpty(records)){
            return result;
        }
        if(CollectionUtils.isEmpty(ids)){
            Set<Long> longs = records.stream().map(CoinRecharge::getUserId).collect(Collectors.toSet());
            ArrayList<Long> idList = new ArrayList<>(longs);
            user = userServiceFeign.getBasicUsers(idList,mobile,userName);
        }
        buildCoinRecharge(user,records);
        return result;
    }
    private void buildCoinRecharge(Map<Long, UserDto> user, List<CoinRecharge> records) {
        for(CoinRecharge coinRecharge : records){
            UserDto userDto = user.get(coinRecharge.getUserId());
            if(userDto != null){
                coinRecharge.setRealName(userDto.getRealName());
                coinRecharge.setUsername(userDto.getUsername());
                coinRecharge.setMobile(userDto.getMobile());
            }else{
                coinRecharge.setRealName("数据为空");
                coinRecharge.setUsername("数据为空");
                coinRecharge.setMobile("数据为空");
            }
        }
    }
}
